﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using Model;
using MySql.Data.MySqlClient;

namespace DAL
{
    public class SOP_CompanyRegisterDAL
    {
        #region 注册
        /// <summary>
        /// 注册
        /// </summary>
        /// <param name="model"></param>
        /// <param name="UserId"></param>
        /// <returns></returns>
        public int Register(SOP_CompanyRegister model, out int UserId)
        {
            UserId = 0;
            using (MySqlConnection conn = DAL.SqlHelper.Settings.GetSOPMySqlConnection(true))
            {
                try
                {
                    MySqlCommand cmd = new MySqlCommand("P_RegisterUser_arcyun", conn);
                    cmd.CommandType = CommandType.StoredProcedure;
                    cmd.Parameters.Add("@Mobile_Para", MySqlDbType.VarChar, 11).Value = model.Mobile;
                    cmd.Parameters.Add("@UserName_Para", MySqlDbType.VarChar, 20).Value = model.UserName;
                    cmd.Parameters.Add("@Password_Para", MySqlDbType.VarChar, 200).Value = model.Password;
                    cmd.Parameters.Add("@Email_Para", MySqlDbType.VarChar, 100).Value = model.Email;
                    cmd.Parameters.Add("@CompanyName_Para", MySqlDbType.VarChar, 200).Value = model.CompanyName;
                    cmd.Parameters.Add("@StoreNumber_Para", MySqlDbType.Int32).Value = model.StoreNumber;
                    cmd.Parameters.Add("@RecommendMobile_Para", MySqlDbType.VarChar, 50).Value = model.RecommendMobile;
                    cmd.Parameters.Add("@IPAddress_Para", MySqlDbType.VarChar, 20).Value = model.IPAddress;
                    cmd.Parameters.Add("@UserAgent_Para", MySqlDbType.VarChar, 200).Value = model.UserAgent;
                    cmd.Parameters.Add("@AddTime_Para", MySqlDbType.DateTime).Value = model.AddTime;
                    cmd.Parameters.Add("@IsSuccess_Para", MySqlDbType.Int32).Value = 0;
                    cmd.Parameters.Add("@UserId_Para", MySqlDbType.Int32).Value = 0;
                    cmd.Parameters["@IsSuccess_Para"].Direction = ParameterDirection.Output;
                    cmd.Parameters["@UserId_Para"].Direction = ParameterDirection.Output;
                    if (conn.State == ConnectionState.Closed)
                    {
                        conn.Open();
                    }
                    cmd.ExecuteNonQuery();
                    Object IsSuccessObj = cmd.Parameters["@IsSuccess_Para"].Value;
                    int isSuccess = IsSuccessObj == null || IsSuccessObj.ToString() == string.Empty ? -5 : (int)IsSuccessObj;
                    if (!string.IsNullOrWhiteSpace(cmd.Parameters["@UserId_Para"].Value.ToString()))
                        UserId = Convert.ToInt32(cmd.Parameters["@UserId_Para"].Value.ToString());
                    return isSuccess;

                }
                catch (Exception ex)
                {
                    ActCommon.Log.WriteLog("Register(date:'" + System.DateTime.Now + "'),error:" + ex.Message, "AddCompany");
                    return -1000;
                }
                finally
                {
                    conn.Close();
                }
            }
        }
        #endregion

        #region 登录
        /// <summary>
        /// 登录
        /// </summary>
        /// <param name="Mobile"></param>
        /// <param name="Password"></param>
        /// <returns></returns>
        public SOP_CompanyRegister Login(string Mobile, string Password, string IPAddress, string UserAgent)
        {
            SOP_CompanyRegister model = null;
            using (MySqlConnection conn = DAL.SqlHelper.Settings.GetSOPMySqlConnection(false))
            {
                try
                {
                    MySqlCommand cmd = new MySqlCommand("P_UserLogin_arcyun", conn);
                    cmd.CommandType = CommandType.StoredProcedure;
                    cmd.Parameters.Add("@Mobile_Para", MySqlDbType.VarChar, 11).Value = Mobile;
                    cmd.Parameters.Add("@Password_Para", MySqlDbType.VarChar, 200).Value = Password;
                    cmd.Parameters.Add("@IPAddress_Para", MySqlDbType.VarChar, 20).Value = IPAddress;
                    cmd.Parameters.Add("@UserAgent_Para", MySqlDbType.VarChar, 200).Value = UserAgent;
                    cmd.Parameters.Add("@AddTime_Para", MySqlDbType.DateTime).Value = System.DateTime.Now;

                    if (conn.State == ConnectionState.Closed)
                    {
                        conn.Open();
                    }

                    MySqlDataReader sdr = cmd.ExecuteReader(CommandBehavior.CloseConnection);
                    if (sdr != null)
                    {
                        while (sdr.Read())
                        {
                            model = new SOP_CompanyRegister();
                            model.UserId = Convert.ToInt32(sdr["UserId"].ToString());
                            model.Mobile = sdr["Mobile"].ToString();
                            model.UserName = sdr["UserName"].ToString();
                            model.Email = sdr["Email"].ToString();
                            model.CompanyName = sdr["CompanyName"].ToString();
                            if (!string.IsNullOrWhiteSpace(sdr["StoreNumber"].ToString()))
                                model.StoreNumber = Convert.ToInt32(sdr["StoreNumber"].ToString());
                            else
                                model.StoreNumber = 0;
                            if (!string.IsNullOrWhiteSpace(sdr["LastLoginTime"].ToString()))
                                model.LastLoginTime = Convert.ToDateTime(sdr["LastLoginTime"].ToString());

                            if (!string.IsNullOrWhiteSpace(sdr["AddTime"].ToString()))
                                model.AddTime = Convert.ToDateTime(sdr["AddTime"].ToString());
                            model.IPAddress = sdr["IPAddress"].ToString();
                            model.UserAgent = sdr["UserAgent"].ToString();
                        }
                    }

                    return model;
                }
                catch (Exception ex)
                {
                    ActCommon.Log.WriteLog("Login(date:'" + System.DateTime.Now + "'),error:" + ex.Message, "AddCompany");
                    return null;
                }
                finally
                {
                    conn.Close();
                }
            }
        }
        #endregion

        #region 获取列表
        public SOP_CompanyRegister GetUserInfo(int UserId)
        {
            SOP_CompanyRegister model = null;
            using (MySqlConnection conn = DAL.SqlHelper.Settings.GetSOPMySqlConnection(false))
            {
                try
                {
                    StringBuilder strSql = new StringBuilder();
                    strSql.Append("select * from SOP_CompanyRegister where UserId=" + UserId + "");
                    MySqlCommand cmd = new MySqlCommand(strSql.ToString(), conn);
                    cmd.CommandType = CommandType.Text;


                    if (conn.State == ConnectionState.Closed)
                    {
                        conn.Open();
                    }

                    MySqlDataReader sdr = cmd.ExecuteReader(CommandBehavior.CloseConnection);
                    if (sdr != null)
                    {
                        while (sdr.Read())
                        {
                            model = new SOP_CompanyRegister();
                            model.UserId = Convert.ToInt32(sdr["UserId"].ToString());
                            model.Mobile = sdr["Mobile"].ToString();
                            model.UserName = sdr["UserName"].ToString();
                            model.Email = sdr["Email"].ToString();
                            model.CompanyName = sdr["CompanyName"].ToString();
                            if (!string.IsNullOrWhiteSpace(sdr["StoreNumber"].ToString()))
                                model.StoreNumber = Convert.ToInt32(sdr["StoreNumber"].ToString());
                            else
                                model.StoreNumber = 0;
                            if (!string.IsNullOrWhiteSpace(sdr["LastLoginTime"].ToString()))
                                model.LastLoginTime = Convert.ToDateTime(sdr["LastLoginTime"].ToString());

                            if (!string.IsNullOrWhiteSpace(sdr["AddTime"].ToString()))
                                model.AddTime = Convert.ToDateTime(sdr["AddTime"].ToString());
                            model.IPAddress = sdr["IPAddress"].ToString();
                            model.UserAgent = sdr["UserAgent"].ToString();
                        }
                    }

                    return model;
                }
                catch (Exception ex)
                {
                    ActCommon.Log.WriteLog("GetUserInfo(date:'" + System.DateTime.Now + "'),error:" + ex.Message, "AddCompany");
                    return null;
                }
                finally
                {
                    conn.Close();
                }
            }
        }
        #endregion

        #region 修改密码
        /// <summary>
        /// 修改密码
        /// </summary>
        /// <param name="Mobile"></param>
        /// <param name="Password"></param>
        /// <param name="IPAddress"></param>
        /// <param name="UserAgent"></param>
        /// <returns></returns>
        public int UpdatePasswordByMobile(string Mobile, string Password, string IPAddress, string UserAgent, out string UserName, out int UserId)
        {
            UserName = "";
            UserId = 0;
            using (MySqlConnection conn = DAL.SqlHelper.Settings.GetSOPMySqlConnection(true))
            {
                try
                {
                    MySqlCommand cmd = new MySqlCommand("P_SetPasswordByMobile_arcyun", conn);
                    cmd.CommandType = CommandType.StoredProcedure;
                    cmd.Parameters.Add("@Mobile_Para", MySqlDbType.VarChar, 11).Value = Mobile;
                    cmd.Parameters.Add("@Password_Para", MySqlDbType.VarChar, 200).Value = Password;
                    cmd.Parameters.Add("@IPAddress_Para", MySqlDbType.VarChar, 20).Value = IPAddress;
                    cmd.Parameters.Add("@UserAgent_Para", MySqlDbType.VarChar, 200).Value = UserAgent;
                    cmd.Parameters.Add("@AddTime_Para", MySqlDbType.DateTime).Value = System.DateTime.Now;
                    cmd.Parameters.Add("@IsSuccess_Para", MySqlDbType.Int32).Value = 0;
                    cmd.Parameters.Add("@OutUserName_Para", MySqlDbType.VarChar, 50).Value = "";
                    cmd.Parameters.Add("@UserId_Para", MySqlDbType.Int32).Value = 0;

                    cmd.Parameters["@IsSuccess_Para"].Direction = ParameterDirection.Output;
                    cmd.Parameters["@OutUserName_Para"].Direction = ParameterDirection.Output;
                    cmd.Parameters["@UserId_Para"].Direction = ParameterDirection.Output;
                    if (conn.State == ConnectionState.Closed)
                    {
                        conn.Open();
                    }
                    cmd.ExecuteNonQuery();
                    Object IsSuccessObj = cmd.Parameters["@IsSuccess_Para"].Value;
                    int isSuccess = IsSuccessObj == null || IsSuccessObj.ToString() == string.Empty ? -5 : (int)IsSuccessObj;
                    if (isSuccess == 5)
                    {
                        UserName = cmd.Parameters["@OutUserName_Para"].Value.ToString();
                        if (!string.IsNullOrWhiteSpace(cmd.Parameters["@UserId_Para"].Value.ToString()))
                            UserId = Convert.ToInt32(cmd.Parameters["@UserId_Para"].Value.ToString());
                    }
                    return isSuccess;

                }
                catch (Exception ex)
                {
                    ActCommon.Log.WriteLog("UpdatePasswordByMobile(date:'" + System.DateTime.Now + "'),error:" + ex.Message, "AddCompany");
                    return -1000;
                }
                finally
                {
                    conn.Close();
                }
            }
        }
        #endregion

        #region 获取云管理登录密码
        public string GetPasswordByUserName(string UserName, string StaffCode)
        {
            string strPassword = "";
            using (MySqlConnection conn = DAL.SqlHelper.Settings.GetBrandMySqlConnection(false))
            {
                try
                {
                    MySqlCommand cmd = new MySqlCommand("P_GetPasswordByUserName_arcyun", conn);
                    cmd.CommandType = CommandType.StoredProcedure;
                    cmd.Parameters.Add("@UserName_Para", MySqlDbType.VarChar, 50).Value = UserName;
                    cmd.Parameters.Add("@StaffCode_Para", MySqlDbType.VarChar, 50).Value = StaffCode;

                    if (conn.State == ConnectionState.Closed)
                    {
                        conn.Open();
                    }

                    MySqlDataReader sdr = cmd.ExecuteReader(CommandBehavior.CloseConnection);
                    if (sdr != null)
                    {
                        while (sdr.Read())
                        {
                            strPassword = sdr["Password"].ToString();
                        }
                    }
                    return strPassword;
                }
                catch (Exception ex)
                {
                    ActCommon.Log.WriteLog("GetPasswordByUserName(date:'" + System.DateTime.Now + "'),error:" + ex.Message, "AddCompany");
                    return "";
                }
                finally
                {
                    conn.Close();
                }
            }
        }
        #endregion
    }
}
